package just.stuff.morsels.ui.user;

import just.stuff.morsels.MorselApplication;
import just.stuff.morsels.MorselSession;
import just.stuff.morsels.objects.User;

import org.apache.wicket.IRequestTarget;
import org.apache.wicket.request.RequestParameters;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;
import org.apache.wicket.request.target.coding.AbstractRequestTargetUrlCodingStrategy;

public final class LoginCallbackHandler extends
		AbstractRequestTargetUrlCodingStrategy {
	public LoginCallbackHandler(String mountPath) {
		super(mountPath);
	}

	public boolean matches(IRequestTarget requestTarget) {
		return false;
	}

	public CharSequence encode(IRequestTarget requestTarget) {
		throw new UnsupportedOperationException();
	}

	public IRequestTarget decode(RequestParameters requestParameters) {
		// TODO: handle unsuccessful login
		String email = ((String[]) requestParameters.getParameters().get(
				"openid.ext1.value.email"))[0];
		String returnUrl = MorselSession.get().getLoginRedirectUrl();
		User user = MorselApplication.dataSource().login( email );
		MorselSession.get().setUser(user);
		return new RedirectRequestTarget(returnUrl);
	}
}